<template>
  <div v-loading="loading">
    <div class="search-box">
      <div class="search-form">
        <el-form ref="searchForm" :inline="true" :model="searchForm">
          <el-form-item label="业务名称" prop="processDefKey">
            <el-select v-model="searchForm.procDefKey" clearable style="width:180px;">
              <el-option
                v-for="item in procDefKeyOptions"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="onSearch">查询</el-button>
            <el-button icon="el-icon-remove-outline" title="重置" @click="resetSearchForm" />
          </el-form-item>
        </el-form>
      </div>
      <div class="toolbar-box clearfix">
        <el-dropdown @command="handleAdd">
          <el-button type="primary" icon="el-icon-circle-plus" @click="add">新增申请<i class="el-icon-arrow-down el-icon--right" /></el-button>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item v-if="checkPermission(['award:apply'])" command="award"><i class="el-icon-medal" />荣誉</el-dropdown-item>
            <el-dropdown-item v-if="checkPermission(['training:apply'])" command="training"><i class="el-icon-connection" />学习交流</el-dropdown-item>
            <el-dropdown-item v-if="checkPermission(['socialJob:apply'])" command="socialJob"><i class="el-icon-collection" />社会任职</el-dropdown-item>
            <el-dropdown-item v-if="checkPermission(['publish:apply'])" command="publish"><i class="el-icon-chat-line-square" />教师发表</el-dropdown-item>
            <el-dropdown-item v-if="checkPermission(['leave:apply'])" command="leave"><i class="el-icon-video-pause" />员工请假</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </div>
    </div>

    <el-table
      ref="multipleTable"
      stripe
      :data="tableData"
      border
      class="table"
    >
      <el-table-column prop="procDefKey" label="业务名称">
        <template slot-scope="scope">
          {{ scope.row.procDefKey|toProcessName }}
        </template>
      </el-table-column>
      <el-table-column prop="name" label="申请标题" />
      <el-table-column prop="state" label="流程状态" />
      <el-table-column prop="startTime" label="申请时间" width="170">
        <template slot-scope="scope">
          <span>{{ scope.row.startTime | parseTime("{y}-{m}-{d} {h}:{i}") }}</span>
        </template>
      </el-table-column>

      <el-table-column prop="approveTime" label="审批时间" width="170">
        <template slot-scope="scope">
          <span>{{ scope.row.approveTime | parseTime("{y}-{m}-{d} {h}:{i}") }}</span>
        </template>
      </el-table-column>

      <el-table-column prop="statusName" label="状态" />
      <el-table-column label="操作" width="190" align="left">
        <template slot-scope="scope">
          <el-button v-if="scope.row.status===1 && scope.row.procDefKey!=='publish'" type="text" icon="el-icon-edit" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button v-if="scope.row.status===1" type="text" icon="el-icon-delete" class="red" @click="handleCancel(scope.row)">取消</el-button>
          <el-button type="text" icon="el-icon-view" @click="handleShow(scope.row)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination :total="total" :page.sync="curPage" :limit.sync="limit" @pagination="getList" />
    <awardForm ref="awardForm" />
    <leaveForm ref="leaveForm" />
    <publishForm ref="publishForm" />
    <trainingForm ref="trainingForm" />
    <socialjobForm ref="socialjobForm" />
  </div>
</template>

<script>
import request from '@/utils/request'
import Pagination from '@/components/Pagination'
import { processKeys } from '@/api/basedata'
import checkPermission from '@/utils/permission'
import awardForm from '../../edu/award/apply/form'
import leaveForm from '../../edu/leave/apply/form'
import publishForm from '../../edu/publish/apply/form'
import trainingForm from '../../edu/training/apply/form'
import socialjobForm from '../../edu/socialjob/apply/form'
export default {
  name: 'ProcessRunning',
  components: { Pagination, awardForm, leaveForm, publishForm, trainingForm, socialjobForm },
  data() {
    return {
      curPage: 1,
      limit: 5,
      total: 0,
      loading: true,
      downloadLoading: false,
      orderByClause: null,
      procDefKeyOptions: processKeys,
      tableData: [],
      searchForm: {
        procDefKey: ''
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    checkPermission,
    // 获取数据
    getList() {
      this.loading = true
      const param = {}
      const queryPage = {}
      queryPage.limit = this.limit
      queryPage.offset = this.limit * (this.curPage - 1)
      param.page = queryPage
      if (this.orderByClause && this.orderByClause.trim() !== '') {
        param.orderByClause = this.orderByClause
      }
      this.copyNotBlankField(this.searchForm, param)
      request({ url: '/processInst/findRunningProcess', method: 'post', data: param })
        .then(res => {
          this.loading = false
          this.tableData = res.rows
          this.total = res.total
          this.curPage = res.pageNumber
        })
        .catch(e => {
          this.loading = false
          this.$message.error('查询出错')
        })
    },
    onSearch() {
      this.getList()
    },
    resetSearchForm() {
      this.$refs['searchForm'].resetFields()
    },
    handleAdd(key) {
      if (key === 'award') {
        this.$refs.awardForm.add()
      } else if (key === 'leave') {
        this.$refs.leaveForm.add()
      } else if (key === 'training') {
        this.$refs.trainingForm.add()
      } else if (key === 'socialJob') {
        this.$refs.socialjobForm.add()
      } else if (key === 'publish') {
        this.$refs.publishForm.add()
      }
    },
    handleEdit(row) {
      if (row.procDefKey === 'award') {
        this.$refs.awardForm.edit(row.businessKey)
      } else if (row.procDefKey === 'leave') {
        this.$refs.leaveForm.edit(row.businessKey)
      } else if (row.procDefKey === 'training') {
        this.$refs.trainingForm.edit(row.businessKey)
      } else if (row.procDefKey === 'socialJob') {
        this.$refs.socialjobForm.edit(row.businessKey)
      } // else if (row.procDefKey === 'publish') {
      // this.$refs.publishForm.edit(row.businessKey)
      // }
    },
    handleCancel(row) {
      this.$confirm('此操作将删除[' + row.name + '], 是否继续?', '确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.doCancel(row)
        })
    },
    doCancel(row) {
      this.loading = true
      let url = ''
      if (row.procDefKey === 'award') {
        url = '/edu/award/cancel'
      } else if (row.procDefKey === 'training') {
        url = '/edu/training/cancel'
      } else if (row.procDefKey === 'socialJob') {
        url = '/edu/socialjob/cancel'
      } else if (row.procDefKey === 'publish') {
        url = '/edu/publish/cancel'
      } else if (row.procDefKey === 'leave') {
        url = '/edu/leave/cancel'
      } else {
        this.loading = false
        return
      }
      request({ url: url, method: 'get', params: { id: row.businessKey }})
        .then(res => {
          this.loading = false
          this.getList()
        })
        .catch(e => {
          this.loading = false
          this.$message.error('取消出错！' + e.message ? e.message : '')
        })
    },
    handleShow(row) {
      this.$parent.$parent.$parent.handleShow(row)
    }

  }
}
</script>
